package com.dance.sharevalidator.validation.validator;

import com.dance.sharevalidator.validation.MultipleOfThree;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Collection;

/**
 * 针对Collection类型的验证
 */
public class MultipleOfThreeForCollection implements ConstraintValidator<MultipleOfThree, Collection> {

    @Override
    public void initialize(MultipleOfThree constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Collection collection, ConstraintValidatorContext constraintValidatorContext) {
        if (null == collection){
            return true;
        }
        return collection.size() % 3 ==0;
    }
}
